<!DOCTYPE html>
<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>${name_china}管理</title>
<%@include file="/common/head.jsp" %>
<link href="<%=request.getContextPath()%>/static/plugin/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet">
<script src="<%=request.getContextPath()%>/static/plugin/zTree/js/jquery.ztree.all-3.5.min.js"></script>
<style>
	#content {
        position: absolute;
        top: 0;
        left: 240px;
        right: 10px;
        bottom: 0;
        box-sizing: border-box;
    }
    ul.ztree {
       width: 230px;
    }
</style>
</head>
<body class="iframe-page">
    <div class="tree-wrap">
    <div class="tree-title">试卷分类列表</div>
    <div class="tree-content">
	    <ul id="treeList" class="ztree"></ul>
	</div>
	</div>
	
	
	<div id="content">
  		<iframe id="treeFrame" frameborder="0" style="width: 100%;height: 100%"></iframe>
	</div>

<script>
	var treeFrame = document.getElementById('treeFrame'),
		currentNode = "",
		ztree;
	$(function() {
		initTree('treeList',CONST_URL.LIST,'id','parentId');
		//默认选中第一个
	});
	
	// 加载组织机构树
	function  initTree(treeId,url,id,pId) {
		var setting = {
			data: {
				simpleData: {
					enable: true,
					idKey: id,
					pIdKey: pId
				}
			},
			async: {
				enable: true
			},
			edit: {
				enable: false,
				drag: {
					isMove: false
				}
			},
			callback: {
				onClick: listBrand
			}
		};
		$.sajax({
			url: url + '?random=' + Math.random() * 1000000,
			onLoadSuccess: function(data) {
				ztree = $.fn.zTree.init($('#' + treeId),setting,data.data);
				expandNode();
				//选中一个节点 、
				var nodes = ztree.getNodes();   
				var childNodes = ztree.transformToArray(nodes[0]);   
				//alert(childNodes[1].id);
				ztree.selectNode(childNodes[1]); 
				var url = CONST_URL.INIT_LIST + "?categoryId=" + childNodes[1].id;
				treeFrame.src = url;
			}
		});
	}
	
	 // 展开树节点
    function expandNode() {
       var nodeCode = '${param.expandCode}',
           node;
       if (nodeCode) {
           // 获取需要自动展开的节点
           node = ztree.getNodeByParam('id', nodeCode, null);
       } else {
           // nodeId为空,展开根节点
           node = ztree.getNodeByParam('parentId', null, null);
       }
       // 如果该节点不包含子节点,则展开其父节点
       if (node && !node.isParent) {
          node = node.getParentNode(); 
       }
       ztree.expandNode(node, true, false, true);
       
    }
	
	
	function listBrand(event, treeId, treeNode){
		var url = CONST_URL.INIT_LIST + "?categoryId=" + treeNode.id;
		if(!treeNode.pId) {
			url = CONST_URL.INIT_LIST + "?categoryId=" + treeNode.id;
		}
		treeFrame.src = url;
	}
	
	// 刷新页面
	function refreshList(expandCode, isContinue) {
		var url = CONST_URL.INIT_MAIN ;
		// 继续添加
		if (isContinue) {
			var node = ztree.getNodeByParam("code", expandCode, null);
            ztree.setting.async.url = CONST_URL.GET_TREE + "?pCode=" + expandCode;
            node.isParent = true;
            ztree.updateNode(node);
            ztree.reAsyncChildNodes(node, "refresh");
		} else {
		   window.location.href = url + '?expandCode=' + expandCode;  
		}
	}
	
	//请求url
	var CONST_URL = {
		INIT_MAIN: basePath + "${common_url}init", 
		INIT_LIST: basePath + "${common_url}initList",
	    LIST: basePath + "${parent_common_url}listTree",
	    GET_TREE: basePath + "${parent_common_url}getTree",
	    INIT_ADD: basePath + "${common_url}initAdd",
	    INIT_EDIT: basePath + "${common_url}initEdit",
	    DEL: basePath + "${common_url}del"
	};
</script>
</body>
</html>
